package app.goods.info.constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import app.goods.info.services.domain.CataLogue;
import app.goods.info.services.domain.CatalogueGoodsNames;
import app.goods.info.services.domain.GoodsInfo;
import app.goods.info.services.domain.ShopInfo;
import app.goods.info.utils.FileReaderUtils;
import cn.hutool.json.JSONUtil;

public class GlobalCache {
	private static final Logger LOGGER = LoggerFactory.getLogger(GlobalCache.class);
	private static volatile GlobalCache instance;

	private GlobalCache() {
	}

	public static GlobalCache getInstance() {
		if (instance == null) {
			synchronized (GlobalCache.class) {
				if (instance == null) {
					instance = new GlobalCache();
				}
			}
		}
		return instance;
	}

	private String loginToken = "";

	private List<CataLogue> targetCataLogues = new ArrayList<CataLogue>();

	private List<CatalogueGoodsNames> targetGoodsNames = new ArrayList<>();
	private List<GoodsInfo> allGoodsInfos = new ArrayList<GoodsInfo>();
	private Map<String, List<GoodsInfo>> catalogueGoodsInfos = new HashMap<>();
	private List<ShopInfo> allShopInfos = new ArrayList<ShopInfo>();
	private Set<CataLogue> errorCataLogue = new HashSet<>();
	// 每次调用接口之后，避免请求太频繁，暂停200毫秒
	private Integer waitTime = 200;

	public String getLoginToken() {
		return loginToken;
	}

	public void setLoginToken(String loginToken) {
		this.loginToken = loginToken;
	}

	public List<CataLogue> getTargetCataLogues() {
		return targetCataLogues;
	}

	public void setTargetCataLogues(List<CataLogue> targetCataLogues) {
		this.targetCataLogues = targetCataLogues;
	}

	public List<CatalogueGoodsNames> getTargetGoodsNames() {
		return targetGoodsNames;
	}

	public void setTargetGoodsNames(List<CatalogueGoodsNames> targetGoodsNames) {
		this.targetGoodsNames = targetGoodsNames;
	}

	/**
	 * 初始化静态数据 <br>
	 * 初始化目标公司 <br>
	 * 初始化目标商品名称 <br>
	 * 初始化目标商品分类 <br>
	 */
	public void initAllCache() {
		LOGGER.debug("initAllCache start...");
		initTargetGoodsNames();
		initTargetCatalogues();
		initAllShopInfo();
		LOGGER.debug("initAllCache end...");
	}

	private void initAllShopInfo() {
		getAllShopInfos().clear();
		String jsonString = FileReaderUtils.readFileQuikly(ConstontValue.ALL_SHOP_INFO);
		getAllShopInfos().addAll(JSONUtil.toList(JSONUtil.parseArray(jsonString), ShopInfo.class));
		LOGGER.debug(JSONUtil.toJsonPrettyStr(getAllShopInfos()));
	}

	private void initTargetCatalogues() {
		getTargetCataLogues().clear();
		String jsonString = FileReaderUtils.readFileQuikly(ConstontValue.TARGET_GOODS_CATALOGUES);
		getTargetCataLogues().addAll(JSONUtil.toList(JSONUtil.parseArray(jsonString), CataLogue.class));
		LOGGER.debug(JSONUtil.toJsonPrettyStr(getTargetCataLogues()));
	}

	private void initTargetGoodsNames() {
		getTargetGoodsNames().clear();
		String jsonString = FileReaderUtils.readFileQuikly(ConstontValue.TARGET_GOODS_NAMES);
		getTargetGoodsNames().addAll(JSONUtil.toList(JSONUtil.parseArray(jsonString), CatalogueGoodsNames.class));
		LOGGER.debug(JSONUtil.toJsonPrettyStr(getTargetGoodsNames()));
	}

	public List<GoodsInfo> getAllGoodsInfos() {
		return allGoodsInfos;
	}

	public void setAllGoodsInfos(List<GoodsInfo> allGoodsInfos) {
		this.allGoodsInfos = allGoodsInfos;
	}

	public static void main(String[] args) {
		GlobalCache.getInstance().initAllCache();
		System.out.println("end....");
	}

	public Map<String, List<GoodsInfo>> getCatalogueGoodsInfos() {
		return catalogueGoodsInfos;
	}

	public void setCatalogueGoodsInfos(Map<String, List<GoodsInfo>> catalogueGoodsInfos) {
		this.catalogueGoodsInfos = catalogueGoodsInfos;
	}

	public List<ShopInfo> getAllShopInfos() {
		return allShopInfos;
	}

	public void setAllShopInfos(List<ShopInfo> allShopInfos) {
		this.allShopInfos = allShopInfos;
	}

	public Set<CataLogue> getErrorCataLogue() {
		return errorCataLogue;
	}

	public void setErrorCataLogue(Set<CataLogue> errorCataLogue) {
		this.errorCataLogue = errorCataLogue;
	}

	public Integer getWaitTime() {
		return waitTime;
	}

	public void setWaitTime(Integer waitTime) {
		this.waitTime = waitTime;
	}

}
